package com.forenow.demo.JDBCLogindemo;
import java.sql.*;
import java.util.Scanner;
public class loginImpl implements login {
    final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    final String DB_URL = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    final String USER = "root";
    final String PASS = "123456";
    Connection conn = null;
    PreparedStatement stmt = null;
    Statement stmt1 = null;
    ResultSet rs;
    public static User GetUserByLogin() {
        User u = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入登陆名");
        String username=sc.next();
        u.setUsername(username);
        System.out.println("请输入密码");
        String password=sc.next();
        u.setPassword(password);
        return u;
    }

    public static User GetUserByRegister() {
        User u = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("请注册登陆名");
        String username=sc.next();
        u.setUsername(username);
        System.out.println("请注册密码");
        String password=sc.next();
        u.setPassword(password);
        return u;
    }

    @Override
    public void register(User user) {
        try {
            //载入jdbc驱动程序
            Class.forName(JDBC_DRIVER);
            //通过DriverManager类的getConnection()方法获得表示数据库连接的Connection类对象
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            String sql = "Insert Into user(username,password) values(?,?)"; //sql语句
            String sql_ck = "Select username FROM user";
            //调用Connection对象的prepareStatement方法，创建一个Statement对象的实例。
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,user.getUsername());
            stmt.setString(2,user.getPassword());
            stmt.executeUpdate();
            rs = stmt.executeQuery(sql_ck);
            while (rs.next()) {
                String user2=rs.getString("username");
                String newuser=user.getUsername();
                if (newuser.equals(user2)) {
                    System.out.println("登陆名已经存在，注册失败");
                    break;
                } else {
                    System.out.println("注册成功");
                    break;
                }

            }
            stmt.close();
            rs.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

    }

    @Override
    public void login(User user) {
        try {
            User u = new User();
            //载入jdbc驱动程序
            Class.forName(JDBC_DRIVER);
            //通过DriverManager类的getConnection()方法获得表示数据库连接的Connection类对象
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            String sql = "Select username,password from user";
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt1 = conn.createStatement();
            rs = stmt1.executeQuery(sql);
            while (rs.next()) {
                String user1=rs.getString("username");
                String password1=rs.getString("password");
                if (password1.equals(user.getPassword())&&user1.equals(user.getUsername())) {
                    System.out.println("欢迎"+user.getUsername()+"登陆");
                    break;
                }else{
                    System.out.println("账号或密码错误，登陆失败！");
                    break;
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        loginImpl in = new loginImpl();
        System.out.println("输入1注册----输入2登录");
        int i = input.nextInt();
        switch (i) {
            case 1:
                in.register(loginImpl.GetUserByRegister());
            case 2:
                in.login(loginImpl.GetUserByLogin());
                break;
            default:

        }
    }
}